cmake_minimum_required(VERSION 3.20)

if (BUILD_STATIC_LIB)
    project(qtawesome VERSION 1.0)
else ()
    project(qtawesomeplugin VERSION 1.0)
endif ()

# 寻找cmake模块
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/.cmake/)
#配置通用编译
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if (QT_VERSION VERSION_GREATER_EQUAL "6.3")
    qt_standard_project_setup()
else ()
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_AUTORCC ON)
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
endif ()

#设置版本号
add_definitions(-DFLUENTUI_VERSION=1,7,7,0)

if (BUILD_STATIC_LIB)
    add_definitions(-DFLUENTUI_BUILD_STATIC_LIB)
endif ()

#编译参数设置
option(FLUENTUI_BUILD_STATIC_LIB "Build static library." OFF)

#导入Qt相关依赖包
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core Quick Qml)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Quick)

set(QT_SDK_DIR "${Qt${QT_VERSION_MAJOR}_DIR}/../../..")
cmake_path(SET QT_SDK_DIR NORMALIZE ${QT_SDK_DIR})

#设置QML插件输出目录，可以通过外部设置，如果外部没有设置就默认到<QT_SDK_DIR_PATH>\qml\QtAwesome目录下
set(QML_PLUGIN_DIRECTORY "" CACHE PATH "Path to QtAwesome plugin")
if (NOT QML_PLUGIN_DIRECTORY)
    set(QML_PLUGIN_DIRECTORY ${QT_SDK_DIR}/qml/QtAwesome)
endif ()

set_source_files_properties(IconFont.qml PROPERTIES
    QT_QML_SINGLETON_TYPE TRUE
)
#遍历所有qml文件
file(GLOB_RECURSE QML_PATHS *.qml qmldir)
foreach (filepath ${QML_PATHS})
    if (${filepath} MATCHES "./")
        string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" filename ${filepath})
        if (${filename} MATCHES "qmldir")
            list(APPEND resource_files ${filename})
        else ()
            list(APPEND qml_files ${filename})
        endif ()
    endif ()
endforeach (filepath)

#遍历所有资源文件
file(GLOB_RECURSE RES_PATHS *.png *.jpg *.svg *.ico *.ttf *.webp *.js *.qm)
foreach (filepath ${RES_PATHS})
    if (${filepath} MATCHES "./")
        string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" filename ${filepath})
        list(APPEND resource_files ${filename})
    endif ()
endforeach (filepath)

#修改资源文件导出路径
foreach (filepath IN LISTS qml_files resource_files)
    string(REPLACE "./" "" filename ${filepath})
    set_source_files_properties(${filepath} PROPERTIES QT_RESOURCE_ALIAS ${filename})
endforeach ()

if (BUILD_STATIC_LIB)
    set(LIB_TYPE "STATIC")
else ()
    set(LIB_TYPE "SHARED")
endif ()

if (BUILD_STATIC_LIB)
    set(PLUGIN_TARGET_NAME "")
else ()
    #如果是动态库，则使用插件目标作为其自己的支持目标来定义 QML 模块，在这种情况下，模块必须在运行时动态加载，并且不能由其他目标直接链接到
    set(PLUGIN_TARGET_NAME ${PROJECT_NAME})
endif ()
qt_add_library(${PROJECT_NAME} ${LIB_TYPE})
qt_add_qml_module(${PROJECT_NAME}
        PLUGIN_TARGET ${PLUGIN_TARGET_NAME}
        OUTPUT_DIRECTORY ${QML_PLUGIN_DIRECTORY}
        VERSION 1.0
        URI "QtAwesome"
        #修改qmltypes文件名称。默认fluentuiplugin.qmltypes，使用默认名称有时候import FluentUI 1.0会爆红，所以修改成plugins.qmltypes
        TYPEINFO "plugins.qmltypes"
        SOURCES ${sources_files}
        QML_FILES ${qml_files}
        RESOURCES ${resource_files}
        RESOURCE_PREFIX "/qt/qml"
)



#去掉mingw生成的动态库libxxx前缀lib，不去掉前缀会导致 module "QtAwesome" plugin "QtAwesomeplugin" not found
if (MINGW)
    set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "")
    #解决编译器报 too many sections的问题
    target_compile_options(${PROJECT_NAME} PRIVATE -Wa,-mbig-obj)
endif ()

#MSVC Debug 添加后缀d，与Qt插件风格保持一致
if (MSVC)
    set_target_properties(${PROJECT_NAME} PROPERTIES DEBUG_POSTFIX "d")
endif ()

#链接库
target_link_libraries(${PROJECT_NAME}
    PUBLIC
    Qt${QT_VERSION_MAJOR}::Quick)

if(APPLE)
    find_library(CARBON_LIBRARY Carbon)
    target_link_libraries(${PROJECT_NAME} PRIVATE ${CARBON_LIBRARY})
elseif(WIN32)
    target_link_libraries(${PROJECT_NAME} PRIVATE user32)
elseif(UNIX)
    if(QT_VERSION_MAJOR STREQUAL "6")
        if(QT_VERSION VERSION_LESS "6.2.0")
            message(FATAL_ERROR "Qt 6.2.0 or greater is required when using Qt6")
        endif()
    else()
        if(QT_VERSION_MAJOR LESS "6")
            find_package(Qt5 REQUIRED COMPONENTS X11Extras)
            target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::X11Extras)
        endif()
    endif()
    target_link_libraries(${PROJECT_NAME} PRIVATE X11)
endif()

#安装
install(DIRECTORY ${QML_PLUGIN_DIRECTORY} DESTINATION ${CMAKE_INSTALL_PREFIX}/imports)
